//
//  WNUserDefaultsManager.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit
import SwiftyUserDefaults

extension DefaultsKeys {
    var userId              : DefaultsKey<String>{.init("userId", defaultValue: "")}
    var appleId             : DefaultsKey<String>{.init("appleId", defaultValue: "")}
    var userName            : DefaultsKey<String>{.init("userName", defaultValue: "")}
    var realName            : DefaultsKey<String>{.init("realName", defaultValue: "")}
    var avatar              : DefaultsKey<String>{.init("avatar", defaultValue: "")}
    var birthday            : DefaultsKey<String>{.init("birthday", defaultValue: "")}
    var sex                 : DefaultsKey<Int>{.init("sex", defaultValue: 0)}
    var gold                : DefaultsKey<Int>{.init("gold", defaultValue: 0)}
    var isVip               : DefaultsKey<Int>{.init("isVip", defaultValue: 0)}

    var firstLand           : DefaultsKey<Bool>{.init("firstLand", defaultValue: true)}
    var isAgreePrivacy      : DefaultsKey<Bool>{.init("isAgreePrivacy", defaultValue: false)}
    
    var darkMode            : DefaultsKey<Int>{.init("darkMode", defaultValue: 0)}
    var readConfig          : DefaultsKey<[String: Any]>{.init("readConfig", defaultValue: [:])}
    var history             : DefaultsKey<[String]>{.init("history", defaultValue: [])}
    var hasCheck            : DefaultsKey<Bool>{.init("hasCheck", defaultValue: false)}
    
    var tapAD               : DefaultsKey<Bool>{.init("tapAD", defaultValue: false)}
    var adJson              : DefaultsKey<[String: Any]>{.init("adJson", defaultValue: [:])}
    var isDeeplink          : DefaultsKey<Bool>{.init("isDeeplink", defaultValue: false)}
    
    var autoOpen            : DefaultsKey<Bool>{.init("autoOpen", defaultValue: true)}
    var enableNot           : DefaultsKey<Bool>{.init("enableNot", defaultValue: false)}
    
    var upgradeClose        : DefaultsKey<Bool>{.init("upgradeClose", defaultValue: false)}
    var upgradeDate         : DefaultsKey<Int>{.init("upgradeDate", defaultValue: 0)}
    
    var environment         : DefaultsKey<Bool>{.init("environment", defaultValue: true)}
    var adid                : DefaultsKey<String>{.init("adid", defaultValue: "")}
    var adBookId            : DefaultsKey<String>{.init("adBookId", defaultValue: "")}
    var adTime              : DefaultsKey<Double>{.init("adTime", defaultValue: 0.0)}
}

/// 保存用户信息
func saveUser(user: UserDetailModel?) {
    Defaults.userId = user?.userId ?? ""
    Defaults.appleId = user?.apple_id ?? ""
    Defaults.userName = user?.userName ?? ""
    Defaults.realName = user?.realName ?? ""
    Defaults.avatar = user?.avatar ?? ""
    Defaults.birthday = user?.birthday ?? ""
    Defaults.sex = user?.sex ?? 0
    Defaults.gold = user?.gold ?? 0
    Defaults.isVip = user?.isVip ?? 0
}

func getUser() -> UserDetailModel? {
    var user = UserDetailModel()
    user.userId = Defaults.userId
    user.apple_id = Defaults.appleId
    user.userName = Defaults.userName
    user.realName = Defaults.realName
    user.avatar = Defaults.avatar
    user.birthday = Defaults.birthday
    user.sex = Defaults.sex
    user.gold = Defaults.gold
    user.isVip = Defaults.isVip
    return user
}

/// 判断用户是否苹果登录
func isAppleLogin() -> Bool {
    if isBlank(Defaults.appleId) {
        return false
    }
    return true
}

/// 判断用户是否登录
func isLogin() -> Bool {
    if isBlank(Defaults.userId) {
        return false
    }
    return true
}

///获取首次下载
func getFirstLand() -> Bool {
    return Defaults.firstLand
}

func saveFirstLand(_ isFirstLand: Bool) {
    Defaults.firstLand = isFirstLand
}

/// 获取用户是否同意隐私条款
func getIsAgreePrivacy() -> Bool {
    return Defaults.isAgreePrivacy
}

func saveIsAgreePrivacy(_ isAgree: Bool) {
    Defaults.isAgreePrivacy = isAgree
}

/// 观看模式
func getDarkMode() -> Int {
    return Defaults.darkMode
}

func saveDarkMode(_ darkMode: Int) {
    Defaults.darkMode = darkMode
}

/// 观看阅读配置
func getReadConfig() -> [String: Any] {
    return Defaults.readConfig
}

func getReadConfigItem(_ key: String) -> Any {
    return Defaults.readConfig[key] ?? (Any).self
}

func saveReadConfig(_ config: [String: Any]) {
    Defaults.readConfig = config
}

func saveReadConfigItem(_ key: String, _ value: Any) {
    Defaults.readConfig[key] = value
}

/// 获取历史记录
func getHistory() -> [String] {
    return Defaults.history
}

func addHistory(_ historyStr: String) {
    if getHistory().contains(historyStr) {
        for(i, v) in getHistory().enumerated() {
            if v == historyStr {
                Defaults.history.remove(at: i)
            }
        }
    }else {
        if getHistory().count > 9 {
            Defaults.history.remove(at: 9)
        }
    }
    Defaults.history.insert(historyStr, at: 0)
}

func deleteHistory(_ item: Int) {
    Defaults.history.remove(at: item)
}

func clearHistory() {
    Defaults.history = []
}

///获取签到状态
func getHasCheck() -> Bool {
    return Defaults.hasCheck
}

func saveHasCheck(_ hasCheck: Bool) {
    Defaults.hasCheck = hasCheck
}

///获取广告json
func getAdJson() -> [String: Any] {
    return Defaults.adJson
}

func saveAdJson(_ adJson: [String: Any]) {
    Defaults.adJson = adJson
}

///获取AD是否点击跳转
func getTapAD() -> Bool {
    return Defaults.tapAD
}

func saveTapAD(_ tapAD: Bool) {
    Defaults.tapAD = tapAD
}

///是否深度链接
func getIsDeeplink() -> Bool {
    return Defaults.isDeeplink
}

func saveIsDeeplink(_ deeplink: Bool) {
    Defaults.isDeeplink = deeplink
}

/// 自动解锁章节
func getAutoOpen() -> Bool {
    return Defaults.autoOpen
}

func saveAutoOpen(_ open: Bool) {
    Defaults.autoOpen = open
}

/// 推送通知状态
func getEnableNot() -> Bool {
    return Defaults.enableNot
}

func saveEnableNot(_ open: Bool) {
    Defaults.enableNot = open
}

/// 获取版本更新关闭
func getUpgradeClose() -> Bool {
    return Defaults.upgradeClose
}

func saveUpgradeClose(_ isClose: Bool) {
    Defaults.upgradeClose = isClose
}

/// 获取版本更新当前时间
func getUpgradeDate() -> Int {
    return Defaults.upgradeDate
}

func saveUpgradeDate(_ date: Int) {
    Defaults.upgradeDate = date
}

/// 本地开发环境
func getEnvironment() -> Bool {
    return Defaults.environment
}

func saveEnvironment(_ env: Bool) {
    Defaults.environment = env
}

/// 投放广告ID
func getADID() -> String {
    return Defaults.adid
}

func saveADID(_ id: String) {
    Defaults.adid = id
}

/// 投放落地页书ID
func getADBOOKID() -> String {
    return Defaults.adBookId
}

func saveADBOOKID(_ id: String) {
    Defaults.adBookId = id
}

/// 投放落地页时间
func getADTIME() -> Double {
    return Defaults.adTime
}

func saveADTIME(_ time: Double) {
    Defaults.adTime = time
}
